package com.example.hzq.zqb.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.hzq.zqb.R;
import com.example.hzq.zqb.activity.GranddetailActivity;
import com.example.hzq.zqb.bean.GrandgoodsBean;
import com.squareup.picasso.Picasso;
import java.util.List;
import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/14.
 */

public class GrandAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<GrandgoodsBean> list;//保存要显示的数据

    public GrandAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<GrandgoodsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name,permoney,price,count;
        private ImageView pic;

        public ViewHolder(View itemView) {
            super(itemView);
            pic = (ImageView) itemView.findViewById(R.id.grand_pic);
            name = (TextView) itemView.findViewById(R.id.grand_name);
            permoney = (TextView) itemView.findViewById(R.id.grand_permoney);
            price = (TextView) itemView.findViewById(R.id.grand_price);
            count = (TextView) itemView.findViewById(R.id.grand_count);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.granditem, parent, false);
        return new ViewHolder(v);
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final GrandgoodsBean grandGoodsBean = list.get(position);
        if (grandGoodsBean == null)
            return;
        final ViewHolder viewholder = (ViewHolder) holder;
        Picasso.with(context).load( imageurl+ grandGoodsBean.getImg()).placeholder(R.drawable.ic_default_adimage).into(viewholder.pic);
        viewholder.name.setText(grandGoodsBean.getName());
        viewholder.permoney.setText(grandGoodsBean.getPermoney()+"（元/天）");
        viewholder.price.setText("单价："+grandGoodsBean.getPrice()+"元");
        viewholder.count.setText("销量："+grandGoodsBean.getSales()+"件");
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                bundle.putSerializable("grandgood",grandGoodsBean);
                context.startActivity(new Intent(context, GranddetailActivity.class).putExtras(bundle));
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        return list.size();
    }
}
